package com.lib.common.widget.refresh;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.lib.common.R;
import com.lib.framework.widget.WidgetUtil;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.internal.ProgressDrawable;

/**
 * Created by jinbao on 2018/6/21.
 */

public class CustomRefreshFooter extends LinearLayout implements RefreshFooter {
    private ImageView mArrow;
    private ImageView mProgressImage;
    private ProgressDrawable mProgressDrawable;
    private int mProgressDrawableColor;
    private int marrowDrawableColor;

    public CustomRefreshFooter(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomRefreshFooter(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RefreshHeader);
        mProgressDrawableColor = a.getColor(R.styleable.RefreshHeader_progressDrawableColor, context.getResources().getColor(R.color.white));
        marrowDrawableColor = a.getColor(R.styleable.RefreshHeader_arrowDrawableColor, context.getResources().getColor(R.color.white));
        a.recycle();
        mArrow = new ImageView(context);
        mProgressImage = new ImageView(context);

        mProgressDrawable = new ProgressDrawable();
        mProgressDrawable.setColor(mProgressDrawableColor);

        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mArrow.setLayoutParams(params);
        mArrow.setImageResource(R.mipmap.ic_refresh_arrow_up_light);
        mArrow.setColorFilter(marrowDrawableColor);
        mArrow.animate().rotation(180);


        params = new LayoutParams(WidgetUtil.dpTopx(context, 20), WidgetUtil.dpTopx(context, 20));
        mProgressImage.setLayoutParams(params);


        mProgressImage.setImageDrawable(mProgressDrawable);

        addView(mArrow);
        addView(mProgressImage);
        mProgressImage.setVisibility(View.GONE);

        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onReleased(RefreshLayout refreshLayout, int height, int extendHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {

    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
        mProgressImage.setVisibility(VISIBLE);
        Drawable drawable = mProgressImage.getDrawable();
        if (drawable instanceof Animatable) {
            ((Animatable) drawable).start();
        } else {
            mProgressImage.animate().rotation(36000).setDuration(100000);
        }
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        if (success) {
            final View progressView = mProgressImage;
            Drawable drawable = mProgressImage.getDrawable();
            if (drawable instanceof Animatable) {
                if (((Animatable) drawable).isRunning()) {
                    ((Animatable) drawable).stop();
                }
            } else {
                progressView.animate().rotation(0).setDuration(0);
            }
            progressView.setVisibility(GONE);
        }
        return 200;
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case None:
            case PullUpToLoad:
                mArrow.setVisibility(VISIBLE);
                mArrow.animate().rotation(0);
                break;
            case ReleaseToLoad:
                mArrow.animate().rotation(180);
                break;
            case Loading:
                mArrow.setVisibility(GONE);
                break;
        }
    }

    @Override
    public boolean setNoMoreData(boolean noMoreData) {
        return false;
    }
}
